package com.sys.controller;

import com.sys.dto.LoginRequest;
import com.sys.dto.RegisterRequest;
import com.sys.dto.ResetPasswordRequest;
import com.sys.dto.SendVerifyCodeRequest;
import com.sys.entity.SysUser;
import com.sys.exception.CustomException;
import com.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Map<String, Object> register(@RequestBody RegisterRequest registerRequest) {
        SysUser user = new SysUser();
        user.setPhone(registerRequest.getPhone());
        user.setPassword(registerRequest.getPassword());
        user.setNickName(registerRequest.getChildName() + "家长"); // 孩子姓名 + "家长"
        user.setChildName(registerRequest.getChildName());
        user.setChildAge(registerRequest.getChildAge());
        SysUser registeredUser = userService.register(user);

        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("userId", registeredUser.getUserId());
        return response;
    }

    @PostMapping("/login")
    public Map<String, String> login(@RequestBody LoginRequest loginRequest) {
        // Basic validation to prevent obvious errors
        if (!StringUtils.hasText(loginRequest.getPhone()) || !loginRequest.getPhone().matches("^\\d{11}$")) {
            throw new CustomException("Invalid phone number format.");
        }
        if (!StringUtils.hasText(loginRequest.getPassword()) || loginRequest.getPassword().length() < 6) {
            throw new CustomException("Password must be at least 6 characters long.");
        }
        String token = userService.login(loginRequest.getPhone(), loginRequest.getPassword());
        Map<String, String> response = new HashMap<>();
        response.put("token", token);
        return response;
    }
    
    /**
     * 发送验证码（预留接口）
     */
    @PostMapping("/send-verify-code")
    public Map<String, Object> sendVerifyCode(@RequestBody SendVerifyCodeRequest request) {
        if (!StringUtils.hasText(request.getPhone())) {
            throw new CustomException("Phone number is required");
        }
        
        boolean success = userService.sendVerifyCode(request.getPhone(), request.getType());
        
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", "Verify code sent successfully (simulated)");
        return response;
    }
    
    /**
     * 重置密码
     */
    @PostMapping("/reset-password")
    public Map<String, Object> resetPassword(@RequestBody ResetPasswordRequest request) {
        // 验证参数
        if (!StringUtils.hasText(request.getPhone())) {
            throw new CustomException("Phone number is required");
        }
        if (!StringUtils.hasText(request.getNewPassword())) {
            throw new CustomException("New password is required");
        }
        
        boolean success = userService.resetPassword(
            request.getPhone(), 
            request.getVerifyCode(), 
            request.getNewPassword()
        );
        
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "Password reset successfully" : "Password reset failed");
        return response;
    }
}